----------------------------------Spell Variables--------------------------------------
SPELL.m_iSpellID = 1300 -- Whatever value you want as long as it doesn't override any other spells' IDs.
SPELL.m_iRange = 1024
SPELL.m_sName = "Frostbolt"
SPELL.m_sToolTip =	[[Launch a ball of frozen
					  rock at the target, dealing
					  20 frost damage and slowing it.]]
SPELL.m_sIcon = "devin/blackbetty/spells/mage/frostbolt.png"
SPELL.m_iCoolDown = 2.5
SPELL.m_iManaCost = 50
SPELL.m_bRequiresTarget = true
SPELL.CastTime = 2.25
SPELL.CastSound = nil
SPELL.m_iSpellType = SPELLTYPE_MAGICDAMAGE


---------------------------------------------------------------------------------------

function SPELL:Cast( pl, target )
	if(pl.DoingCast)then
		return true
	end
	local weapon = pl:GetWeapon("weapon_all")
	local range = self.m_iRange
	local castingloop = "devin/blackbetty/weapons/travelsounds/IceMissileLoop.wav"
	local spellhit = "devin/blackbetty/weapons/hit/Ice1.wav"

	if(target && target:IsValid() && target != pl && pl:GetPos():Distance(target:GetPos()) < self.m_iRange) then
		pl:SetNWString("HoldType", "camera")
		self.CastSound = CreateSound( pl, "devin/blackbetty/weapons/castingloops/Frost.wav" )
		self.CastSound:Play()
		pl.DoingCast = true
		pl.CastWhileMoving = true
		net.Start( "SendPlayerCast" )
		local SendTable = {self.CastTime, self.m_sName, Color(15, 15, 155, 155)}
		net.WriteTable(SendTable)
		net.Send(pl)
		
		print("Casted "..self.m_sName.."!")
		ParticleEffectAttach( "frost_hands", PATTACH_POINT_FOLLOW, pl, pl:LookupAttachment("anim_attachment_RH"))
		timer.Simple(self.CastTime-0.35, function() 
			pl:SetNWBool("Casting", false)
			pl:SetNWBool("Casted", false)
			if(pl and pl:IsValid() and pl:IsPlayer())then
				pl:DoAnimationEvent(ACT_GMOD_GESTURE_MELEE_SHOVE_2HAND) 
			elseif(pl and pl:IsValid() and !pl:IsPlayer())then
					pl:RestartGesture(ACT_RANGE_ATTACK_THROW)
			else
				pl.DoingCast = false
				self.CastSound:Stop()
				pl:StopParticles()
			end
		end)
		timer.Create("casting_"..pl:GetName().."", self.CastTime, 1, function()
			if !pl or !target then
				pl.DoingCast = false
				self.CastSound:Stop()
				pl:StopParticles()
				return true
			end
			if pl.DoingCast then
				pl:EmitSound("devin/blackbetty/weapons/castsounds/IceCast.wav", 100, 100+math.random(-10,10))
				pl.DoingCast = false
				self.CastSound:Stop()
				pl:StopParticles()
				local myhitdelay = ((pl:GetPos() - target:GetPos()):Length()/1800)
				ParticleEffect( "pummel_hit", pl:GetPos() + (pl:GetAngles():Forward()*48) + Vector(0, 0, 45) + (pl:GetAngles():Right()*16), Angle(0,0,0), pl )
				LaunchProjectile(pl, target, 0, "impact_frostbig", "", "frost_hands", castingloop, myhitdelay, 1300, "models/crossbow_bolt.mdl")
				timer.Simple(myhitdelay, function()
					local dmginfo = DamageInfo()
						if(weapon)then
							dmginfo:SetDamage( math.random(weapon.MinWeaponDamage*3, weapon.MaxWeaponDamage*3) )
						else
							dmginfo:SetDamage( math.random(pl.MinDamage, pl.MaxDamage) ) 
						end
						dmginfo:SetDamageType( DMG_DROWN ) 
						dmginfo:SetAttacker( pl )
					target:TakeDamageInfo( dmginfo )
					if(pl:IsPlayer())then
						UpdatePlayerResource(pl, 5)
					end
					target:EmitSound(spellhit, 140, 100+math.random(-10,10))
					ParticleEffect("impact_frostbig", target:GetPos() + Vector(0, 0, 45), Angle(0,0,0), target )
				end)
			end
		end)
	else
		pl:EmitSound(spellhit, 100, 100+math.random(-10,10))
	end
end




